﻿(function(window, owner) {
	/** 令牌缓存数据 */
	owner.accessToken_cache = "accessToken_cache";
	/** 配置信息是否已经成功获取标识 */
	owner.isLoadConfig = false;

	/** 令牌对象 */
	owner.Token = {
		/** 根据key和secret获取到的临时令牌 */
		accessToken: "at.2r0ph9im2m3icu5hd8yn931z0yoe2zi4-4jlubaofso-1qiv88s-ikjbppygy",
		/** 令牌的有效期限 */
		expireTime: ""
	}

	/** 设备对象（获取设备通道信息） */
	owner.CameraData = {
		/** 令牌信息 */
		accessToken: "",
		/** 设备序列号 */
		deviceSerial: "",
		/** 设备通道号 */
		channelNo: 1
	}

	/** 云台控制对象 */
	owner.CloudData = {
		/** 令牌信息 */
		accessToken: "",
		/** 设备序列号 */
		deviceSerial: "",
		/** 通道号 */
		channelNo: 1,
		/** 操作命令（0-上，1-下，2-左，3-右，4-左上，5-左下，6-右上，7-右下，8-放大，9-缩小，10-近焦距，11-远焦距） */
		direction: "",
		/** 云台速度（0-慢，1-适中，2-快） */
		speed: 1
	}

	/** 云台控制停止对象 */
	owner.CloudStopData = {
		/** 令牌信息 */
		accessToken: "",
		/** 设备序列号 */
		deviceSerial: "",
		/** 通道号 */
		channelNo: 1
	}

	/** 获取令牌的地址 */
//	owner.accessTokenURL = "http://sensor.sureserve.cn/interfaces/token_handler.ashx?action=getysytoken";
	owner.accessTokenURL = baseURL() + "/token_handler.ashx?action=getysytoken";
	/** 获取设备通道地址 */
	owner.cameraChannelURL = "https://open.ys7.com/api/lapp/device/camera/list";
	/** 云台开启控制地址 */
	owner.cloudstartControlUrl = "https://open.ys7.com/api/lapp/device/ptz/start";
	/** 云台停止控制地址 */
	owner.cloudstopControlUrl = "https://open.ys7.com/api/lapp/device/ptz/stop";
	/** 获取设备能力集地址 */
	owner.cameraCapacityUrl = "https://open.ys7.com/api/lapp/device/capacity";

	/** 获取令牌（勿频繁使用，否则会被拉入黑名单） */
	owner.GetAccessToken = function() {
		//  提取缓存令牌对像，不存在则重新获取
		//  存在缓存则比较过期时间，过期则重新获取，未过期直接使用
		
		var token = localStorage.getItem(this.accessToken_cache);
		var flag = false;
		if(token) {
			this.Token = JSON.parse(token);
			var expireTime = this.Token.expireTime;
			var nowtime = new Date();
			nowtime = nowtime.getFullYear() + '-' + ((nowtime.getMonth() + 1) < 10 ? ('0' + (nowtime.getMonth() + 1)) : (nowtime.getMonth() + 1))  + '-' + (nowtime.getDate() < 10 ? ('0' + nowtime.getDate()) : nowtime.getDate()) + ' ' + (nowtime.getHours() < 10 ? ('0' + nowtime.getHours()) : nowtime.getHours()) + ':' + (nowtime.getMinutes() < 10 ? ('0' + nowtime.getMinutes()) : nowtime.getMinutes()) + ':' + (nowtime.getSeconds() < 10 ? ('0' + nowtime.getSeconds()) : nowtime.getSeconds());

			console.log(expireTime); 
			console.log(nowtime);
//			alert(expireTime < nowtime);
			if(expireTime < nowtime) {
				flag = true;
			}
		} else {
			flag = true;
		}
		if(flag) {
			mui.post(owner.accessTokenURL, function(d) {
				
				d = JSON.parse(d);
				if(d.result == 0) {
					owner.Token.accessToken = d.data.accessToken;
					owner.Token.expireTime = d.data.expireTime;
					localStorage.setItem(owner.accessToken_cache, JSON.stringify(owner.Token));
					owner.isLoadConfig = true;
				}
			});
		}
		owner.isLoadConfig = true;
	}

	/** 设置摄像头配置信息 */
	owner.SetCameraConfig = function(serialno, channelno) {
		owner.CameraData.channelNo = channelno;
		owner.CameraData.deviceSerial = serialno;
		if(owner.isLoadConfig) {
			owner.CameraData.accessToken = owner.Token.accessToken;
		} else {
			owner.GetAccessToken();
		}
	}

	/** 获取指定设备的通道号（备用） */
	owner.GetCameraChannel = function() {
		owner.CameraData.accessToken = owner.Token.accessToken;
		owner.CameraData.deviceSerial = "719433745";
		mui.post(owner.cameraChannelURL, owner.CameraData, function(d) {
			var flag = owner.ErrorLog(d.code);
			if(flag) {
				console.log(d.data);
			}
		});
	}

	/** 获取设备能力集（备用） */
	owner.GetCameraCapacity = function() {
		if(!owner.isLoadConfig) {
			owner.GetAccessToken();

			setTimeout(function() {
				owner.GetCameraCapacity();
			}, 500);
		} else {
			mui.post(owner.cameraCapacityUrl, owner.CameraData, function(d) {
				var flag = owner.ErrorLog(d.code);
				if(flag) {
					console.log(d.data);
				}
			});
		}
	}

	/** 开启云台控制
	 * @method StartCloudControl
	 * @param {字符串} serialno 设备序列号
	 * @param {整形} channelno 设备通道号
	 * @param {整形} direction 操作命令（0-上，1-下，2-左，3-右，4-左上，5-左下，6-右上，7-右下，8-放大，9-缩小，10-近焦距，11-远焦距）
	 */
	
	owner.StartCloudControl = function(direction) {
		owner.CloudData.direction = direction;
		owner.CloudData.accessToken = owner.Token.accessToken;
		owner.CloudData.deviceSerial = owner.CameraData.deviceSerial;
		owner.CloudData.channelNo = owner.CameraData.channelNo;
		mui.post(owner.cloudstartControlUrl, owner.CloudData, function(d) {
			owner.dataCode = d;
			var flag = owner.ErrorLog(d.code);
			if (flag) {
			    // 当放大或者缩小操作时， 每点击一下延时1秒发送停止指令，默认控制会一直放大
			    if(direction == 8 || direction == 9){
			    	setTimeout(function () {
				        owner.StopCloudControl();
				    }, 800);
			    }
			}
		});
	}

	/** 停止云台控制 */
	owner.StopCloudControl = function() {
		owner.CloudStopData.deviceSerial = owner.CameraData.deviceSerial;
		owner.CloudStopData.channelNo = owner.CameraData.channelNo;
		owner.CloudStopData.accessToken = owner.Token.accessToken;
		mui.post(owner.cloudstopControlUrl, owner.CloudStopData, function(d) {
			var flag = owner.ErrorLog(d.code);
			SuccessTips(d.code);
			if(flag) {
				setTimeout(function() {}, 1000);
			}
		});
	}

	/** 打印错误日志 */
	owner.ErrorLog = function(code) {
		switch(code) {
			case "200":
				console.log("请求成功!");
				return true;
			case "10001":
				console.log("参数错误!");
				break;
			case "10002":
				console.log("accessToken异常或过期!");
				break;
			case "10005":
				console.log("appKey被冻结!");
				break;
			case "20002":
				console.log("设备不存在!");
				break;
			case "20006":
				console.log("检查设备网络状况，稍后再试!");
				break;
			case "20007":
				console.log("检查设备是否在线!");
				break;
			case "20008":
				console.log("操作过于频繁，稍后再试!");
				break;
			case "20014":
				console.log("设备序号错误!");
				break;
			case "20018":
				console.log("检查设备是否属于当前账户!");
				break;
			case "49999":
				console.log("接口调用异常!");
				break;
			case "60000":
				console.log("设备不支持云台控制!");
				break;
			case "60001":
				console.log("用户无云台控制权限!");
				break;
			case "60006":
				console.log("云台当前操作失败!");
				break;
			case "60009":
				console.log("正在调用预置点!");
				break;
			case "60020":
				console.log("不支持该命令!");
				break;
		}
		return false;
	}
}(window, window.YCYCloud = {}));